let userInfo = null;
getUserInfo();
$(function() {

    if (userInfo) {
        getAvatar(userInfo)
    }
    $("#loginout").on("click", function() {
        // e.preventDefault();
        let layer = layui.layer;
        layer.confirm('确定退出登陆吗?', { icon: 3, title: '提示' }, function(index) {
            localStorage.removeItem("token");
            location.href = "login.html"

            layer.close(index);
        });
    })

})

function getUserInfo() {
    $.ajax({

        type: "get",
        url: "/my/userinfo",
        async: false,

        success: function(res) {
            if (res.status !== 0) return layer.msg(res.message)
                // getAvatar(res.data);
            userInfo = res.data;
            console.log(userInfo);

        }

    });
}

function getAvatar(user) {
    let html = user.nickname || user.username;
    $("#welcome").html("欢迎您&nbsp;&nbsp;" + html);
    let str = html.substr(0, 1).toUpperCase();

    if (user.user_pic !== null) {
        $(".layui-nav-img").attr("src", user.user_pic).show();
        $(".text-avatar").html(str).hide();

    } else {
        $(".layui-nav-img").attr("src", user.user_pic).hide();
        $(".text-avatar").html(str).show();

    }


}